package com.nf.action;

import com.nf.entity.Account;
import com.nf.entity.Record;
import com.nf.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.security.pkcs11.Secmod;
import org.springframework.web.bind.support.SessionStatus;
@Controller
@SessionAttributes("account")
public class AccountAction {


    @Autowired
    private AccountService accountService;

    @ModelAttribute("account")
    Account getAccount(){
        return null;
    }
    //首页
    @RequestMapping("/index")
    public String index(@ModelAttribute("account") Account account, Model model){

        System.out.println(account);
        if (account==null){
            model.addAttribute("message","LOGIN_TIME_OUT");
            return "redirect:loginView";
        }
        return "index";
    }
    //登录界面
    @RequestMapping("/loginView")
    public String loginView(){
        return "login";
    }

    //登录数据提交到后台判断
    @RequestMapping("/loginCheck")
    public String loginCheck(String userName,String pwd,Model model) {
        Account account = accountService.login(userName, pwd);
        System.out.println(userName+"  "+pwd);
        if (account == null) {
           model.addAttribute("message","LOGIN_ERROR");
           return "redirect:loginView";
        } else {
            model.addAttribute("account", account);
            return "redirect:index";
        }
    }
//查询余额
    @RequestMapping("/queryBanlanc")
    public String queryBanlanc(@ModelAttribute("account") Account account,Model model) {

        if (account == null) {
            model.addAttribute("message", "LOGIN_ERROR");
            return "redirect:loginView";
        }
            Integer banlance = accountService.queryBanlanceById(account.getId());
            model.addAttribute("banlance", banlance);
            return "queryBanlanc";

    }

    //修改密码界面
    @RequestMapping("/updatePasswordView")
    public String updatePassword(@ModelAttribute("account") Account account,Model model,String oldPwd,String newPwd,String newPwds){

        if (account==null){
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";
        }
        return "updatePasswordView";
    }

    //修改密码的action

    @RequestMapping("/updatePassWord")
    public String updatePassword(@ModelAttribute("account")Account account,String oldPwd,String newPwd,String newPwds,Model model){
        if (account==null){
            //return "redirect:loginView?message=login_error";
            //等同
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";

        }
        //校验（属于控制器的校验，不是service的校验）
        if (newPwd.equals(oldPwd)){
            model.addAttribute("message","OLD_EQUAL_NEW");
            return "redirect:updatePasswordView";
        }

        if (newPwd.length()>6){
            model.addAttribute("message","PASSWORD_LONG");
            return "redirect:updatePasswordView";
        }
        if (!newPwd.equals(newPwds)){
            model.addAttribute("message","P1_NOT_EQUAL_P2");
            return "redirect:updatePasswordView";
        }
        boolean flag = accountService.updatePassword(newPwd, oldPwd, account.getId());
        if(flag) {
            return "updatePassword_success";
        }else{
            return "updatePassword_error";
        }
    }
    //取钱页面
    @RequestMapping("/getMoneyView")
    public String getMoneyView(@ModelAttribute("account")Account account,Model model){
      if (account==null){
          model.addAttribute("message","LOGIN_ERROR");
          return "redirect:loginView";
      }
        return "getMoney";
    }

    //取钱的action

    @RequestMapping("/getMoney")
    public String getMoney(@ModelAttribute("account") Account account,Model model,Integer money) {
        if (account == null) {
            model.addAttribute("message", "LOGIN_ERROR");
            return "redirect:loginView";
        }
        System.out.println(account.getId() + " " + money + " ");
        boolean flag = accountService.getMoney(money, account.getId());
        if (flag == false) {
            model.addAttribute("message", "LOGIN_TIME_OUT");
            return "getMoney_error";

        }
        model.addAttribute("money", money);
        return "getMoney_succse";
    }

    //转账页面

    @RequestMapping("/TransactionView")
    public String TransactionView(@ModelAttribute("account")Account account,Model model){
        if (account==null){
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";
        }
        return "TransactionRecord";
    }
    //转出action
    @RequestMapping("/Transaction")
    public String Transaction(@ModelAttribute("account") Account account,Model model,Integer userId,Integer trMoney){
        if (account==null){
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";
        }
        boolean flag=accountService.transMoney(trMoney,userId,account.getId());
        if (flag==false){
            return "TransactionRecord_error";
        }
        model.addAttribute("message","success");
        model.addAttribute("trMoney",trMoney);

        return "TransactionRecord_success";
    }

    //存钱的view
    @RequestMapping("/saveMoneyView")
    public String saveMoneyView(@ModelAttribute("account")Account account,Model model){
        if (account==null){
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";
        }
        return "SaveMoney";
    }

    //存钱的action
    @RequestMapping("/saveMoney")
    public String savaMoney(@ModelAttribute("account") Account account,Model model,Integer savemoney){
        if (account==null){
            model.addAttribute("message","LOGIN_ERROR");
            return "redirect:loginView";
        }
        boolean flag=accountService.saveMoney(savemoney,account.getId());
        if (flag){
            return "saveMoney_suucces";
        }
        return "saveMoney_error";
    }

    @RequestMapping("/exit")
    public String exit(@ModelAttribute("account") Account account, SessionStatus sessionStatus){
        //account = null;
        //System.out.println("account:"+account);
        sessionStatus.setComplete();
        //return "redirect:index";
        return "redirect:loginView";
    }


    // 以下注释的都是无用代码
 /**/
 //    //查询一条交易记录
//    @RequestMapping("/query")
//    public String query(Integer id,Integer money,Model model){
//
//        Integer banlance = accountService.queryBanlanceById(id);
//        model.addAttribute("banlanc",banlance);
//
//        System.out.println(banlance);
//        return "index";
//    }
//
//
////修改密码
//    @ResponseBody
//    @RequestMapping("/modifypwd")
//    public Record test3(){
//        boolean b = accountService.updatePassword("999999","12345",4);
//        return null;
//    }
//
//
//    //存钱
//    @ResponseBody
//    @RequestMapping("/savemoney")
//    public Record test4(){
//        boolean b = accountService.saveMoney(500,4);
//        return null;
//    }
//
//    @RequestMapping("/savemoneys")
//    public String save(Integer savemoney,Model model,Integer userId){
//
//        boolean b=accountService.saveMoney(savemoney,userId);
//        if(b==false){
//            return "save_Error";
//        }else {
//            return "index";
//        }
//    }
//
//    //取钱
//    @RequestMapping("/getmoney")
//    public String test5(){
//        boolean b = accountService.getMoney(300,4);
//        return "index";
//    }
//
    //转账

//    @RequestMapping("/transmoney")
//    public String  test6(){
//        boolean b = accountService.transMoney(480,1,4);
//        return "TransactionRecord";
//    }
//    @RequestMapping("/transmoneys")
//    public String trans(Integer money,Integer userid,Integer userMyId){
//
//        boolean b=accountService.transMoney(100,6,1);
//
//
//        if (b==false){
//            System.out.println("失败");
//        }else
//            System.out.println("成功");
//        return "index";
//    }


}
